package cn.xz.mxxz.activity


import android.os.Handler
import android.widget.SeekBar
import androidx.recyclerview.widget.LinearLayoutManager
import cn.xz.mxxz.R
import cn.xz.mxxz.adapter.LocalTAdapter
import cn.xz.mxxz.adapter.OpenTAdapter
import cn.xz.mxxz.base.BaseActivity
import cn.xz.mxxz.bean.*
import cn.xz.mxxz.databinding.ActivityOpenBinding
import cn.xz.mxxz.dialog.AllNowDialog
import cn.xz.mxxz.dialog.LocalListDialog
import cn.xz.mxxz.net.ControlDevice
import cn.xz.mxxz.net.OkHttpUtil
import cn.xz.mxxz.net.SendUdp
import cn.xz.mxxz.util.*

/**
 * 开合屏
 * 暂无视频，预留
 * 一分屏  二分屏 八分屏
 * 1： 播放暂停
 * 2:  两个屏分别播放暂停  静音 放音
 * 8 查看返回 上一页 下一页
 * */
class OpenActivity : BaseActivity<ActivityOpenBinding>(R.layout.activity_open) {

    //标识符
    private var mTag = 0
    //设备信息
    private var mDevice: DeviceList? = null
    //八分屏的时候当前选中的屏幕
    private var mP = 0
    //当前视频进度值
    private var pMove = 0
    private var handler: Handler? = null
    //当前多个设备的集合
    private var areaList: IntArray = intArrayOf(0,1,2,3,4,5,6,7,8)
    //当前功能type
    private val mWsType = "dq_foldable"

    override fun initData() {
        //获取当前设备信息
        mTag = intent.getIntExtra(CommonLib.JUMP_TYPE, -1)
        if (mTag != -1){
            mDevice = ControlDevice.getThisDevice(mTag)
            if (CommonLib.MY_LOCAL_IP.isNotEmpty() && mDevice != null){
                mDevice!!.ip = CommonLib.MY_LOCAL_IP
            }
        }
        //如果数据异常直接退出
        if (mTag == -1 || mDevice == null){
            ToastUtil.show("当前数据异常")
            finish()
        }else{
            //设置标题
            mDataBind.tvTitle.text = ControlDevice.getDeviceName(mTag)
            //设置分类
            setArea1()
            //获取当前音量
            getVoice()
            //开启定时器获取视频进度
            /*handler = Handler()
            handler?.post(movieR)*/
        }
    }


    override fun initEvent() {
        //返回
        mDataBind.tvBack.setOnClickListener {
            finish()
        }
        //播放
        mDataBind.zkPlay.setOnClickListener {
            setPlay(1)
        }
        //暂停
        mDataBind.zkPause.setOnClickListener {
            setPlay(2)
        }
        //重播
        mDataBind.zkReplay.setOnClickListener {
            setPlay(3)
        }
        //最大化
        mDataBind.tvShow.setOnClickListener {
            setPlay(12)
        }
        //最小化
        mDataBind.tvHide.setOnClickListener {
            setPlay(11)
        }
        //列表
        mDataBind.zkList.setOnClickListener {
            getLocalList()
        }
        //全屏上一页
        mDataBind.tvQS.setOnClickListener {
            val mBody = OpenBeanIT.Params()
            mBody.id = 10
            mBody.type = 1
            postIT("pageControl",mBody)
        }
        //全屏下一页
        mDataBind.tvQX.setOnClickListener {
            val mBody = OpenBeanIT.Params()
            mBody.id = 10
            mBody.type = 2
            postIT("pageControl",mBody)
        }
        //全屏上一条
        mDataBind.tvP1St.setOnClickListener {
            val mBody = OpenBeanIT.Params()
            mBody.id = 10
            mBody.type = 3
            postIT("pageControl",mBody)
        }
        //全屏下一条
        mDataBind.tvP1Xt.setOnClickListener {
            val mBody = OpenBeanIT.Params()
            mBody.id = 10
            mBody.type = 4
            postIT("pageControl",mBody)
        }
        //八分屏 上一页
        mDataBind.tv8S.setOnClickListener {
            //setPlay(9)
            val mBody = OpenBeanIT.Params()
            mBody.id = mP
            mBody.type = 1
            postIT("pageControl",mBody)
        }
        //八分屏 下一页
        mDataBind.tv8X.setOnClickListener {
            //setPlay(10)
            val mBody = OpenBeanIT.Params()
            mBody.id = mP
            mBody.type = 2
            postIT("pageControl",mBody)
        }
        //鼠键
        mDataBind.zkSj.setOnClickListener {
            ARouter.to(SJActivity::class.java,mDevice!!.ip)
        }
        //声音滑动监听
        mDataBind.zkVolumeSeekbar.setOnSeekBarChangeListener(object :
            SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                SeekBarPopUtils.move(progress, seekBar, false)
                LocalHttp.changeVoice(mDevice!!.ip,progress)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
                SeekBarPopUtils.showPop(seekBar)
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                SeekBarPopUtils.dismiss()
            }
        })
        //视频滑动监听
        mDataBind.zkVideoSeekbar.setOnSeekBarChangeListener(object :
            SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                SeekBarPopUtils.move(progress, seekBar, true)
                pMove = progress
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
                SeekBarPopUtils.showPop(seekBar)
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                SeekBarPopUtils.dismiss()
                LocalHttp.changeMovie(mDevice!!.ip,pMove)
            }
        })
        //一分屏
        mDataBind.tvP1.setOnClickListener {
            setButton(1,true)
        }
        //二分屏
        mDataBind.tvP2.setOnClickListener {
            setButton(2,true)
        }
        //四分屏
        mDataBind.tvP6.setOnClickListener {
            setButton(5,true)
        }
        //五分屏
        mDataBind.tvP4.setOnClickListener {
            setButton(4,true)
        }
        //八分屏
        mDataBind.tvP3.setOnClickListener {
            setButton(3,true)
        }
        //紧急关
        mDataBind.tvStop.setOnClickListener {
            SendUdp.send("stop","192.168.10.16",6000)
        }
        //145分屏  播放
        mDataBind.tvP1Play.setOnClickListener {
            val mBody = OpenBeanIT.Params()
            mBody.id = SessionUtils.getInt(SessionUtils.MY_FP,1)
            mBody.type = 2
            postIT("videoControl",mBody)
        }
        //145分屏 暂停
        mDataBind.tvP1Pause.setOnClickListener {
            val mBody = OpenBeanIT.Params()
            mBody.id = SessionUtils.getInt(SessionUtils.MY_FP,1)
            mBody.type = 1
            postIT("videoControl",mBody)
        }
        //145静音
        mDataBind.tvP1J.setOnClickListener {
            val mBody = OpenBeanIT.Params()
            mBody.id = SessionUtils.getInt(SessionUtils.MY_FP,1)
            mBody.type = 3
            postIT("videoControl",mBody)
        }
        //145取消静音
        mDataBind.tvP1Q.setOnClickListener {
            val mBody = OpenBeanIT.Params()
            mBody.id = SessionUtils.getInt(SessionUtils.MY_FP,1)
            mBody.type = 4
            postIT("videoControl",mBody)
        }
        //二分屏 左 播放
        mDataBind.tvL2P.setOnClickListener {
            val mBody = OpenBeanIT.Params()
            mBody.id = 2
            mBody.type = 2
            postIT("videoControl",mBody)
        }
        //二分屏 左 暂停
        mDataBind.tvL2Z.setOnClickListener {
            val mBody = OpenBeanIT.Params()
            mBody.id = 2
            mBody.type = 1
            postIT("videoControl",mBody)
        }
        //二分屏 左 静音
        mDataBind.tvL2J.setOnClickListener {
            val mBody = OpenBeanIT.Params()
            mBody.id = 2
            mBody.type = 3
            postIT("videoControl",mBody)
        }
        //二分屏 左 取消静音
        mDataBind.tvL2Q.setOnClickListener {
            val mBody = OpenBeanIT.Params()
            mBody.id = 2
            mBody.type = 4
            postIT("videoControl",mBody)
        }
        //二分屏 右 播放
        mDataBind.tvR2P.setOnClickListener {
            val mBody = OpenBeanIT.Params()
            mBody.id = 3
            mBody.type = 2
            postIT("videoControl",mBody)
        }
        //二分屏 右 暂停
        mDataBind.tvR2Z.setOnClickListener {
            val mBody = OpenBeanIT.Params()
            mBody.id = 3
            mBody.type = 1
            postIT("videoControl",mBody)
        }
        //二分屏 右 静音
        mDataBind.tvR2J.setOnClickListener {
            val mBody = OpenBeanIT.Params()
            mBody.id = 3
            mBody.type = 3
            postIT("videoControl",mBody)
        }
        //二分屏 右 取消静音
        mDataBind.tvR2Q.setOnClickListener {
            val mBody = OpenBeanIT.Params()
            mBody.id = 3
            mBody.type = 4
            postIT("videoControl",mBody)
        }
        //八分屏查看
        mDataBind.tv8See.setOnClickListener {
            val mBody = OpenBeanIT.Params()
            mBody.id = mP
            mBody.type = 1
            postIT("viewControl",mBody)
        }
        //八分屏返回
        mDataBind.tv8GoBack.setOnClickListener {
            val mBody = OpenBeanIT.Params()
            mBody.id = mP
            mBody.type = 2
            postIT("viewControl",mBody)
        }
    }

    /*
     * @param ip
     * @param port
     * @param type
     * 1:播放
     * 2:暂停
     * 3:重播
     * 5:上一个
     * 6:下一个
     * 7：上一页
     * 8：下一页
     * 9：列表上一个
     * 10：列表下一个
     * 11：隐藏客户端
     * 12：显示客户端
     */
    private fun setPlay(i :Int){
        LocalHttp.moviePlay(mDevice!!.ip,i)
    }

    private fun getVoice(){
        LocalHttp.getVoiceSize(mDevice!!.ip, LocalHttp.GetVoiceSizeListener {
            mDataBind.zkVolumeSeekbar.post {
                mDataBind.zkVolumeSeekbar.progress = it
            }
        })
    }

    private fun getMove(){
        LocalHttp.getMovieSize(mDevice!!.ip,
            LocalHttp.GetMovieSizeListener { maxTxt, minTxt, maxSeekBar, seekProgress ->
            mDataBind.zkVideoSeekbar.post {
                mDataBind.zkMaxTime.text = maxTxt
                mDataBind.zkMinTime.text = minTxt
                mDataBind.zkVideoSeekbar.max = maxSeekBar
                mDataBind.zkVideoSeekbar.progress = seekProgress
            }
        })
    }

    private var movieR: Runnable = object : Runnable {
        override fun run() {
            getMove()
            handler?.postDelayed(this, 1000)
        }
    }

    //获取本地资源列表
    private fun getLocalList(){
        LocalHttp.getPlayData(this,mDevice!!.ip, LocalHttp.GetPlayDataListener {
            if (it.isNotEmpty()){
                LocalListDialog.newInstance(
                    mDevice!!,
                    it
                ).show(this@OpenActivity.supportFragmentManager,"LocalListDialog")
            }
        })
    }

    private fun setArea1(){
        //获取上次点击的分屏
        setButton(SessionUtils.getInt(SessionUtils.MY_FP,1),false)
        //八分屏八个屏
        if (areaList.isNotEmpty()){
            val mL: ArrayList<AreaList> = arrayListOf()
            areaList.forEach {
                val mStr = if (it == 0){
                    "一键全控"
                }else{
                    "屏${it}"
                }
                mL.add(AreaList(id = it,name = mStr, select = it == 0))
            }

            val ms = LinearLayoutManager(this)
            ms.orientation = LinearLayoutManager.HORIZONTAL
            mDataBind.zkAreaRecycler.layoutManager = ms
            val mAdapter = OpenTAdapter()
            mAdapter.setOnItemClickListener { adapter, view, position ->
                val data = adapter.data as ArrayList<AreaList>
                mP = data[position].id!!
                //点击一键全控要发初始化命令
                if (mP == 0){
                    OkHttpUtil.postUrlWebInit(CommonLib.WEB_IP,CommonLib.WEB_PORT,"init",mWsType)
                }
                data.forEach {
                    it.select = false
                    if (it.id == data[position].id){
                        it.select = true
                    }
                }
                adapter.notifyDataSetChanged()
            }
            mDataBind.zkAreaRecycler.adapter = mAdapter
            mAdapter.setList(mL)
        }
    }

    private fun setButton(i :Int,b: Boolean){
        if (b){
            //记录点击的几分屏
            SessionUtils.commitInt(SessionUtils.MY_FP,i)
            //给WEB发命令
            OkHttpUtil.postUrlWebID(CommonLib.WEB_IP,CommonLib.WEB_PORT,"screenChange",i,mWsType)
        }
        mDataBind.tvP1.background  =  AppHelper.getDrawable(R.mipmap.icon_content)
        mDataBind.tvP2.background  =  AppHelper.getDrawable(R.mipmap.icon_content)
        mDataBind.tvP3.background  =  AppHelper.getDrawable(R.mipmap.icon_content)
        mDataBind.tvP4.background  =  AppHelper.getDrawable(R.mipmap.icon_content)
        mDataBind.tvP6.background  =  AppHelper.getDrawable(R.mipmap.icon_content)
        when(i){
            1->{ //全屏
                mDataBind.llc1.visible()
                mDataBind.llcAllQ.visible()
                mDataBind.llcT1.visible()
                mDataBind.zkAreaRecycler.invisible()
                mDataBind.llc8.invisible()
                mDataBind.llc2.invisible()
                mDataBind.tvP1.background  =  AppHelper.getDrawable(R.mipmap.icon_content_1)
                if (b){
                    SendUdp.send("play1","192.168.10.16",6000)
                }
            }
            2->{ //二分屏
                mDataBind.llc1.invisible()
                mDataBind.llcAllQ.gone()
                mDataBind.llcT1.gone()
                mDataBind.zkAreaRecycler.invisible()
                mDataBind.llc8.invisible()
                mDataBind.llc2.visible()
                mDataBind.tvP2.background  =  AppHelper.getDrawable(R.mipmap.icon_content_1)
                if (b){
                    SendUdp.send("play4","192.168.10.16",6000)
                }
            }
            3->{ //八分屏
                mDataBind.llc1.invisible()
                mDataBind.llcAllQ.gone()
                mDataBind.llcT1.gone()
                mDataBind.zkAreaRecycler.visible()
                mDataBind.llc8.visible()
                mDataBind.llc2.invisible()
                mDataBind.tvP3.background  =  AppHelper.getDrawable(R.mipmap.icon_content_1)
                if (b){
                    SendUdp.send("play0","192.168.10.16",6000)
                }
            }
            4->{ //五分屏
                mDataBind.llc1.visible()
                mDataBind.llcAllQ.gone()
                mDataBind.llcT1.gone()
                mDataBind.zkAreaRecycler.invisible()
                mDataBind.llc8.invisible()
                mDataBind.llc2.invisible()
                mDataBind.tvP4.background  =  AppHelper.getDrawable(R.mipmap.icon_content_1)
                if (b){
                    SendUdp.send("play2","192.168.10.16",6000)
                }
            }
            5->{ //四分屏
                mDataBind.llc1.visible()
                mDataBind.llcAllQ.gone()
                mDataBind.llcT1.gone()
                mDataBind.zkAreaRecycler.invisible()
                mDataBind.llc8.invisible()
                mDataBind.llc2.invisible()
                mDataBind.tvP6.background  =  AppHelper.getDrawable(R.mipmap.icon_content_1)
                if (b){
                    SendUdp.send("play3","192.168.10.16",6000)
                }
            }
        }
    }

    private fun postIT(method: String,body: OpenBeanIT.Params){
        OkHttpUtil.postUrlWebIT(CommonLib.WEB_IP,CommonLib.WEB_PORT,method,body,mWsType)
    }

    override fun onDestroy() {
        super.onDestroy()
        if (handler != null){
            handler?.removeCallbacks(movieR)
            handler = null
        }
    }
}